﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NhanVien_frmQuyTacDe : System.Web.UI.Page
{
    DataClassesDataContext qldt = new DataClassesDataContext();
    clsQtDeThi Ob_qtdt = new clsQtDeThi();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (qldt.tbl_QUYTACDETHIs.Count()!=0)
            {
                this.grvQTDe.SelectedIndex = 0;
                txtMaQuyTac.Text = grvQTDe.SelectedRow.Cells[1].Text;
                txtSoluongCH.Text = grvQTDe.SelectedRow.Cells[2].Text;
                txtKho.Text = grvQTDe.SelectedRow.Cells[3].Text;
                txtTrungbinh.Text = grvQTDe.SelectedRow.Cells[4].Text;
                txtDe.Text = grvQTDe.SelectedRow.Cells[5].Text;
                Trangthaibd();
            }


        }
    }

    public void Hamdong()
    {
        txtMaQuyTac.Enabled = false;
        txtSoluongCH.Enabled = false;
        txtKho.Enabled = false;
        txtTrungbinh.Enabled = false;
        txtDe.Enabled = false;

    }

    public void Trangthaibd()
    {
        btnThem.Text = "Them";
        btnSua.Text = "Sua";
        btnHuyThaotac.Text = "Huy Thao Tac";
        btnThem.Enabled = true;
        btnSua.Enabled = true;
        btnXoa.Enabled = true;

        Hamdong();

    }

    protected void btnThem_Click(object sender, EventArgs e)
    {
        if (btnThem.Text == "Them")
        {
            txtMaQuyTac.Enabled = true;
            txtMaQuyTac.Text = null;
            txtSoluongCH.Enabled = true;
            txtSoluongCH.Text = null;
            txtKho.Enabled = true;
            txtKho.Text = null;
            txtDe.Enabled = true;
            txtDe.Text = null;
            txtTrungbinh.Enabled = true;
            txtTrungbinh.Text = null;


            btnThem.Text = "Luu";
            btnXoa.Enabled = false;
            btnSua.Enabled = false;
        }
        else if (btnThem.Text == "Luu")
        {

            string maqt = txtMaQuyTac.Text;
            int soCH = Convert.ToInt32(txtSoluongCH.Text);
            int Kho = Convert.ToInt32(txtKho.Text);
            int TB = Convert.ToInt32(txtTrungbinh.Text);
            int De = Convert.ToInt32(txtDe.Text);
            int thoigian = Convert.ToInt32(DropDownList1.SelectedItem.Text);
            thoigian *= 60;
            Ob_qtdt.ThemmoiQTDT(maqt, soCH, Kho, TB, De,thoigian);
            grvQTDe.DataBind();


            //rfvnoidung.ControlToValidate = txtNoidungCH.Text;
            // rfvDaA.ControlToValidate = txtA.Text;



            btnThem.Text = "Them";
            Trangthaibd();

        }
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        if (btnSua.Text == "Sua")
        {
            //txtMaCH.Enabled = true;
            txtSoluongCH.Enabled = true;


            txtKho.Enabled = true;
            txtTrungbinh.Enabled = true;
            txtDe.Enabled = true;
            btnSua.Text = "Luu CH";
            btnThem.Enabled = false;
            btnXoa.Enabled = false;
        }
        else if (btnSua.Text == "Luu")
        {

            string maqt = txtMaQuyTac.Text;
            int soCH = Convert.ToInt32(txtSoluongCH.Text);
            int Kho = Convert.ToInt32(txtKho.Text);
            int TB = Convert.ToInt32(txtTrungbinh.Text);
            int De = Convert.ToInt32(txtDe.Text);
            Ob_qtdt.SuaQTde(maqt, soCH, Kho, TB, De);
            grvQTDe.DataBind();
            btnSua.Text = "Sua";
            Trangthaibd();

        }
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        try
        {
            string maqt = grvQTDe.SelectedRow.Cells[1].Text;
            Ob_qtdt.XoaQTde(maqt);
            grvQTDe.DataBind();
        }
        catch { }
    }
    protected void grvQTDe_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtMaQuyTac.Text = grvQTDe.SelectedRow.Cells[1].Text;
        txtSoluongCH.Text = grvQTDe.SelectedRow.Cells[2].Text;
        txtKho.Text = grvQTDe.SelectedRow.Cells[3].Text;
        txtTrungbinh.Text = grvQTDe.SelectedRow.Cells[4].Text;
        txtDe.Text = grvQTDe.SelectedRow.Cells[5].Text;
    }
    protected void btnHuyThaotac_Click(object sender, EventArgs e)
    {
        Trangthaibd();
    }
    public int Kiemtragt(int soluongch, int chdangxuly, int chthu2)
    {
        int kq = soluongch - (chdangxuly + chthu2);
        if (kq < 0)
        {
            return -1;
        }

        return kq;

    }
    protected void txtKho_TextChanged(object sender, EventArgs e)
    {
        int soCH = Convert.ToInt32(txtSoluongCH.Text);
        if (txtKho.Text != string.Empty)
        {
            int Kho = Convert.ToInt32(txtKho.Text);
            if (txtTrungbinh.Text != string.Empty && txtDe.Text.Trim() == string.Empty)
            {
                int TB = Convert.ToInt32(txtTrungbinh.Text);

                if (Kiemtragt(soCH, Kho, TB) == -1)
                {
                    txtDe.Text = string.Empty;
                    txtKho.Text = "Gia tri khong hop le";
                }
                else
                {
                    txtDe.Text = Kiemtragt(soCH, Kho, TB).ToString();

                }


            }
            if (txtTrungbinh.Text == string.Empty && txtDe.Text != string.Empty)
            {
                int De = Convert.ToInt32(txtDe.Text);
                if (Kiemtragt(soCH, Kho, De) == -1)
                {
                    txtTrungbinh.Text = string.Empty;
                    txtKho.Text = "Gia tri khong hop le";
                }
                else
                {
                    txtTrungbinh.Text = Kiemtragt(soCH, Kho, De).ToString();

                }



            }
        }
    }
    protected void txtTrungbinh_TextChanged(object sender, EventArgs e)
    {
        int soCH = Convert.ToInt32(txtSoluongCH.Text);
        if (txtTrungbinh.Text != string.Empty)
        {
            int TB = Convert.ToInt32(txtTrungbinh.Text);
            if (txtKho.Text != string.Empty && txtDe.Text.Trim() == string.Empty)
            {
                int Kho = Convert.ToInt32(txtKho.Text);

                if (Kiemtragt(soCH, Kho, TB) == -1)
                {
                    txtDe.Text = string.Empty;
                    txtTrungbinh.Text = "Gia tri khong hop le";
                }
                else
                {
                    txtDe.Text = Kiemtragt(soCH, Kho, TB).ToString();

                }




            }
            if (txtKho.Text == string.Empty && txtDe.Text != string.Empty)
            {
                int De = Convert.ToInt32(txtDe.Text);

                if (Kiemtragt(soCH, TB, De) == -1)
                {

                    txtKho.Text = string.Empty;
                    txtTrungbinh.Text = "Gia tri khong hop le";
                }
                else
                {
                    txtKho.Text = Kiemtragt(soCH, TB, De).ToString();

                }




            }
        }
    }
    protected void txtDe_TextChanged(object sender, EventArgs e)
    {
        int soCH = Convert.ToInt32(txtSoluongCH.Text);
        if (txtDe.Text != string.Empty)
        {
            int De = Convert.ToInt32(txtDe.Text);
            if (txtKho.Text != string.Empty && txtTrungbinh.Text.Trim() == string.Empty)
            {
                int Kho = Convert.ToInt32(txtKho.Text);

                if (Kiemtragt(soCH, De, Kho) == -1)
                {
                    txtTrungbinh.Text = string.Empty;
                    txtDe.Text = "Gia tri khong hop le";
                }
                else
                {
                    txtTrungbinh.Text = Kiemtragt(soCH, De, Kho).ToString();

                }




            }
            if (txtKho.Text == string.Empty && txtTrungbinh.Text != string.Empty)
            {
                int TB = Convert.ToInt32(txtTrungbinh.Text);
                if (Kiemtragt(soCH, TB, De) == -1)
                {
                    txtKho.Text = string.Empty;
                    txtDe.Text = "Gia tri khong hop le";
                }
                else
                {
                    txtKho.Text = Kiemtragt(soCH, TB, De).ToString();

                }


            }
        }
    }
    protected void txtMaQuyTac_TextChanged(object sender, EventArgs e)
    {
        string mqt = txtMaQuyTac.Text;
        if (Ob_qtdt.KTTrungMa(mqt) == 1)
        {
            txtMaQuyTac.Text = "Trung ma";
            txtMaQuyTac.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            txtMaQuyTac.ForeColor = System.Drawing.Color.Black;
        }
    }
}
